package com.demoworld;

import org.junit.Test;

/**
 * Created by francis on 2017/7/5.
 */
public class BubbleSortTest {
    @Test
    public void test1() {
        bubbleSort(new int[]{34, 12, 56, 783, 2, 43});
    }

    @Test
    public void test2() {
        bubbleSort(new int[]{34, 12, 56, 783, 2, 43, 123});
    }

    @Test
    public void test3() {
        bubbleSort(new int[]{1, 3, 5, 7, 9, 12});
    }

    @Test
    public void test4() {
        bubbleSort(new int[]{12, 9, 7, 5, 3, 1});
    }

    public void bubbleSort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        while (true) {
            boolean finished = true;
            for (int i = 0; i <= arr.length - 1; i++) {
                if (i == arr.length - 1) {
                    break;
                }
                if (arr[i] > arr[i + 1]) {
                    int max = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = max;
                    finished = false;
                }
            }
            if (finished) {
                break;
            }
        }

        for (int i = 0; i <= arr.length - 1; i++) {
            System.out.println(arr[i]);
        }
    }
}
